<?php
require_once(classdir.'commons.php');

class attachments extends commons{
	var
		$tbl = 'attachments';
	function get_items($parent_id, $parent_type,$att_type, $limit=0,$order_by='created_at DESC',$empty_no = false){
		global $db;
		$return = false;
		$this->items = array();
		$parent_id = (int)$parent_id;
		$parent_type = block($parent_type);
		$att_type = (int)$att_type;
		$order_by = block($order_by);
		$sql = "
			SELECT
				id,
				type,
				parent_id,
				parent_type,
				file,
				IS_NULL(original_name,file,original_name) original_name
			FROM
				$this->tbl
			WHERE
				parent_id = '$parent_id'
				AND
				parent_type = '$parent_type'
				AND
				type='$att_type'
		";
		if (is_array($limit)){
			$sql .= " LIMIT ".(int)$limit[0].",".(int)$limit[1]."";
		}elseif((int)$limit>0){
			$sql .= " LIMIT ".(int)$limit."";
		}
		if ($items = $db->query_assoc($sql)){
			$cnt = count($items);
			for($i=0;$i<$cnt;$i++){
				$this->items[] = new attachment($items[$i]);
			}
			$return = true;
		}else{
			$return = false;
		}
		return $return;
	}
	
	function delete($ids=array()){
		$return = true;
		foreach ($this->items as $item){
			if(empty ($ids) || !empty($ids[$item->id])){
				$return &= $item->delete();
			}
		}
		return $return;
	}
}
?>